package com.work.demo.juctest;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

public class UnsafeFactory {
	public static Unsafe getUnsafe() {
		try {
			Field field = Unsafe.class.getDeclaredField("theUnsafe");
			field.setAccessible(true);
			return (Unsafe) field.get(null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static long getFieldOffset(Unsafe unsafe, Class clazz, String fieldName) {
		try {
			return unsafe.objectFieldOffset(clazz.getDeclaredField(fieldName));
		} catch (NoSuchFieldException e) {
			throw new Error(e);
		}
	}
}